﻿var CkSoftware = CkSoftware || {};
CkSoftware.GroupMeChat = CkSoftware.GroupMeChat || {};

CkSoftware.GroupMeChat.MessageViewModel = function (messageData, displayUpdateObservable) {
	var self = this;

	var cleanedMessage = messageData.text;

	self.displayObservable = displayUpdateObservable;

	self.id = ko.observable(messageData.id);
	self.created = ko.observable(messageData.created_at);
	self.userName = ko.observable(messageData.name);
	self.avatarUrl = ko.observable(messageData.avatar_url + '.avatar');
	self.text = ko.observable(cleanedMessage !== null && cleanedMessage !== undefined ? cleanedMessage.replace('\n', '<br />') : cleanedMessage);
	self.attachments = ko.observableArray(ko.utils.arrayMap(messageData.attachments, function (item) { return new CkSoftware.GroupMeChat.AttachmentViewModel(item); }));

	self.createdDateTime = ko.computed(function () {
		self.displayObservable();
		var date = new Date(self.created() * 1000);
		return moment(date).set('seconds', 0).fromNow();
	});
};
